package com.taotao.merchant.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.security.utils.SecurityUtils;
import com.taotao.merchant.domain.Coupon;
import com.taotao.merchant.domain.SkuInfo;
import com.taotao.merchant.domain.request.CouponRequest;
import com.taotao.merchant.domain.request.CouponSearchRequest;
import com.taotao.merchant.domain.response.CouponResponse;
import com.taotao.merchant.mapper.CouponMapper;
import com.taotao.merchant.service.CouponService;
import com.taotao.system.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @className: CouponServiceImpl
 * @author: ZhangYuQi
 * @date: 2025/1/13 下午12:20
 * @Version: 1.0
 * @description:
 */

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;


    @Override
    public PageInfo<Coupon> getCoupons(CouponSearchRequest coupon) {
        //定义分页规则
        PageHelper.startPage(coupon.getPageNum(), coupon.getPageSize());
        List<Coupon> couponList = couponMapper.selectAllCoupons(coupon);
        return new PageInfo<Coupon>(couponList);
    }

    @Override
    public CouponResponse getCouponById(Long id) {
        CouponResponse couponResponse = couponMapper.selectCouponById(id);
        //如果是指定商品可用 查询sku列表
        if(couponResponse.getApplicableProducts()==1){
            List<SkuInfo> skuInfos = couponMapper.selectSkuListByCouponId(id);
            couponResponse.setSkuList(skuInfos);
        }
        return couponResponse;
    }

    @Override
    public void delCouponById(Long id) {
        //逻辑删除商家发布优惠券
        couponMapper.updateDelStatusById(id);
        //逻辑删除中间表
        couponMapper.updateSkuCouponDelStatusById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addCoupon(CouponRequest couponRequest) throws ParseException {

        // 设置格式是 "yyyy-MM-dd HH:mm:ss"
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //切割区间数组 获取使用时间
        String[] intervalTime = couponRequest.getIntervalTime();
        if (intervalTime != null && intervalTime.length > 1) {
            Date beginTime = sdf.parse(intervalTime[0]);
            Date endTime = sdf.parse(intervalTime[1]);
            couponRequest.setUseingBeginTime(beginTime);
            couponRequest.setUseingEndTime(endTime);
        }

        //判断优惠券是否是随机金额
        if (couponRequest.getCouponType() == 2) {
            //如果是随机金额 通过最大值和最小值  获取最终随机金额
            int random = (int) (Math.random() * (couponRequest.getMaxRandom() - couponRequest.getMinRandom() + 1) + couponRequest.getMinRandom());
            couponRequest.setDiscountMoney(random);
        }

        //添加优惠券 返回主键自增id
        couponMapper.insertCoupon(couponRequest);

        //判断该优惠券是否指定商品可用
        if (couponRequest.getApplicableProducts() == 1) {
            //添加指定sku可使用优惠券表
            couponMapper.insertSkuCoupon(couponRequest.getId(), couponRequest.getSkuIds());
        }

    }

    @Override
    public List<SkuInfo> getSkuList() {
        //获取当前登录用户id
        Long userId = SecurityUtils.getUserId();
        return couponMapper.selectSkuListByUserId(userId);
    }

}
